﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Management;
using System.Net;
using System.Text.RegularExpressions;


namespace Common
{

    public class SystemInfo
    {
        private int m_ProcessorCount = 0;   //CPU个数 
        private PerformanceCounter pcCpuLoad;   //CPU计数器 
        private long m_PhysicalMemory = 0;   //物理内存 
        public string OSVersion = "";
        public string NetVersion = "";
        private const int GW_HWNDFIRST = 0;
        private const int GW_HWNDNEXT = 2;
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 268435456;
        private const int WS_BORDER = 8388608;

        #region AIP声明
        [DllImport("IpHlpApi.dll")]
        extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);

        [DllImport("User32")]
        private extern static int GetWindow(int hWnd, int wCmd);

        [DllImport("User32")]
        private extern static int GetWindowLongA(int hWnd, int wIndx);

        [DllImport("user32.dll")]
        private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);

        [DllImport("user32", CharSet = CharSet.Auto)]
        private extern static int GetWindowTextLength(IntPtr hWnd);
        #endregion

        #region 构造函数
        ///  
        /// 构造函数，初始化计数器等 
        ///  
        public SystemInfo()
        {
            try
            {
                //初始化CPU计数器 
                pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                pcCpuLoad.MachineName = ".";
                pcCpuLoad.NextValue();

                //CPU个数 
                m_ProcessorCount = Environment.ProcessorCount;
                OSVersion = System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor;
                NetVersion = Environment.Version.ToString();
                //获得物理内存 
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if (mo["TotalPhysicalMemory"] != null)
                    {
                        m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
                    }
                }
            }
            catch (Exception ex) { }
        }
        #endregion

        #region CPU个数
        ///  
        /// 获取CPU个数 
        ///  
        public int ProcessorCount
        {
            get
            {
                return m_ProcessorCount;
            }
        }
        #endregion

        #region CPU占用率
        ///  
        /// 获取CPU占用率 
        ///  
        public float CpuLoad
        {
            get
            {
                return pcCpuLoad.NextValue();
            }
        }
        #endregion

        #region 可用内存
        ///  
        /// 获取可用内存 
        ///  
        public long MemoryAvailable
        {
            get
            {
                long availablebytes = 0;
                //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory"); 
                //foreach (ManagementObject mo in mos.Get()) 
                //{ 
                //    availablebytes = long.Parse(mo["Availablebytes"].ToString()); 
                //} 
                try
                {
                    ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
                    foreach (ManagementObject mo in mos.GetInstances())
                    {
                        if (mo["FreePhysicalMemory"] != null)
                        {
                            availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
                        }
                    }
                }
                catch (Exception ex) { }
                return availablebytes;
            }
        }
        #endregion

        #region 物理内存
        ///  
        /// 获取物理内存 
        ///  
        public long PhysicalMemory
        {
            get
            {
                return m_PhysicalMemory;
            }
        }
        #endregion


        public string GetAddressIP()
        {
            try
            {
                string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
                Uri uri = new Uri(strUrl);
                WebRequest wr = WebRequest.Create(uri);
                Stream s = wr.GetResponse().GetResponseStream();
                StreamReader sr = new StreamReader(s, Encoding.Default);
                string all = sr.ReadToEnd(); //读取网站的数据
                int i = all.IndexOf("[") + 1;
                string tempip = all.Substring(i, 15);
                string ip = tempip.Replace("]", "").Replace(" ", "");
                return ip;
            }
            catch (Exception ex)
            {
                return "获取失败，请稍后再试";
            }
        }

    }

    public class GetOSystem
    {
        private const string Windows2000 = "5.0";
        private const string WindowsXP = "5.1";
        private const string Windows2003 = "5.2";
        private const string Windows2008 = "6.0";
        private const string Windows7 = "6.1";
        private const string Windows8OrWindows81 = "6.2";
        private const string Windows10 = "10.0";
        private string OSystemName;
        public void setOSystemName(string oSystemName)
        {
            this.OSystemName = oSystemName;
        }
        public string GetSystemName()
        {
            try
            {
                switch (System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor)
                {
                    case Windows2000:
                        setOSystemName("Windows2000");
                        break;
                    case WindowsXP:
                        setOSystemName("WindowsXP");
                        break;
                    case Windows2003:
                        setOSystemName("Windows2003");
                        break;
                    case Windows2008:
                        setOSystemName("Windows2008");
                        break;
                    case Windows7:
                        setOSystemName("Windows7");
                        break;
                    case Windows8OrWindows81:
                        setOSystemName("Windows8.OrWindows8.1");
                        break;
                    case Windows10:
                        setOSystemName("Windows10");
                        break;
                }
            }
            catch (Exception ex) { }
            return OSystemName;
        }
    }
}